package com.emep.changzhi.analyse.http.statistics;


import com.emep.changzhi.analyse.entity.outlet.SysOutletInfo;
import com.emep.changzhi.analyse.model.AqiControllerData;
import com.emep.changzhi.analyse.model.ResultBean;
import com.emep.changzhi.analyse.service.aqi.AqiControlInfoService;
import com.emep.changzhi.analyse.service.outlet.SysOutletInfoService;
import com.emep.changzhi.analyse.service.statistics.DayofGoodAndHeavyPollutionService;
import com.emep.changzhi.analyse.utils.Constant;
import com.emep.changzhi.analyse.utils.DataValidator;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author lqj
 * @version V1.0
 * @Description: 统计今年到目前为止的重污染(优良)天数、和去年同期的重污染(优良)天数,去年全年的重污染(优良)天数
 * @date 2018/2/26 14:50
 */
@RestController
@RequestMapping("stat")
@Api(value="统计天数controller",tags={"统计天数"})
public class DayofGoodAndHeavyPollutionController {


    @Autowired
    private DayofGoodAndHeavyPollutionService dayofGoodAndHeavyPollutionService;

    @Autowired
    private AqiControlInfoService aqiControlInfoService;
    /**
     * 统计重污染天数
     * @param payload
     * @return
     */
    @ApiOperation(value="某段时间内某城市的重污染天数", notes="某段时间内某城市的重污染天数")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cityCode", value = "城市编码", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "startDate", value = "开始时间", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "endDate", value = "结束时间", required = true, dataType = "String",paramType="query"),
    })
    @GetMapping(value = "badday")
    public ResultBean<Integer> sortofbaddays(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload,"cityCode","startDate","endDate");

        //城市名称
        String city = payload.get("cityCode") != null ? payload.get("cityCode").toString():"" ;
        //开始时间
        String starttime = payload.get("startDate")!= null ? payload.get("startDate").toString():"";
        //结束时间
        String endtime = payload.get("endDate")!= null ? payload.get("endDate").toString():"";

        //重污染or优良

        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            return result.ok(dayofGoodAndHeavyPollutionService.dayOfSoFar(city,starttime,endtime,"重度污染"));
        }
        return result;
    }

    /**
     * 统计优良天数
     * @param payload
     * @return
     */
    @ApiOperation(value="某段时间内某城市的优良天数", notes="某段时间内某城市的优良天数")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cityCode", value = "城市编码", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "startDate", value = "开始时间", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "endDate", value = "结束时间", required = true, dataType = "String",paramType="query"),
    })
    @GetMapping(value = "goodday")
    public ResultBean<Integer> sortofgooddays(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload,"cityCode","startDate","endDate");
        //城市名称
        String city = payload.get("cityCode") != null ? payload.get("cityCode").toString():"" ;
        //开始时间
        String starttime = payload.get("startDate")!= null ? payload.get("startDate").toString():"";
        //结束时间
        String endtime = payload.get("endDate")!= null ? payload.get("endDate").toString():"";

        //重污染or优良
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            return result.ok(dayofGoodAndHeavyPollutionService.dayOfSoFar(city,starttime,endtime,"良"));
        }
        return result;
    }


    /**
     * 按照小时数据（6参数）排序国控站点
     * @param payload
     * @return
     */
    @ApiOperation(value="按照小时数据（6参数）排序国控站点", notes="按照小时数据（6参数）排序国控站点")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParam(name = "pollutionName", value = "污染物名称", required = true, dataType = "String", paramType = "query")
    @GetMapping(value = "ranking/sites")
    public ResultBean<Integer> rankingSites(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload,"pollutionName");

        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            return result.ok(dayofGoodAndHeavyPollutionService.rankingSites(payload));
        }
        return result;
    }


    /**
     *
     * 统计不同站点，不同污染物当前时间的值或者选定时间，和前一小时、前一天、上个月同时间值比较的百分比
     * @param payload
     * @return
     */
    @ApiOperation(value="统计不同站点，不同污染物当前时间的值或者选定时间，和前一小时、前一天、上个月同时间值比较的百分比", notes="统计不同站点，不同污染物当前时间的值或者选定时间，和前一小时、前一天、上个月同时间值比较的百分比")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "positionName", value = "监测站名字", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "timePoint", value = "时间", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pollutant", value = "因子", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping(value = "pollution/percent")
    public ResultBean<AqiControllerData> getPercentbySiteAndPollution(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload);
        String positionName = payload.get("positionName")!= null ? payload.get("positionName").toString():"";
        //时间
        String timePoint = payload.get("timePoint")!= null ? payload.get("timePoint").toString():"";
        //因子名字
        String pollutant = payload.get("pollutant")!= null ? payload.get("pollutant").toString():"";


        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            return result.ok(aqiControlInfoService.findAqiControlInfoByAreaIdAndTimePoint(positionName,timePoint,pollutant));
        }
        return result;

    }


    /**
     *
     * 获取指定站点指定污染物不同时间段内浓度
     * @param payload
     * @return
     */
    @ApiOperation(value="获取指定站点指定污染物不同时间段内浓度", notes="获取指定站点指定污染物不同时间段内浓度")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "positionName", value = "监测站名字", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "timePoint", value = "时间", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pollutant", value = "因子", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping(value = "pollution/concentration")
    public ResultBean<List<AqiControllerData>> getConcentration(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload,"positionName","timePoint","pollutant");
        String positionName = payload.get("positionName")!= null ? payload.get("positionName").toString():"";
        //时间
        String timePoint = payload.get("timePoint")!= null ? payload.get("timePoint").toString():"";
        //因子名字
        String pollutant = payload.get("pollutant")!= null ? payload.get("pollutant").toString():"";
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            return result.ok(aqiControlInfoService.findAqiControlInfoByAreaIdAndTimePoint(positionName,timePoint,pollutant));
        }
        return result;

    }

}
